/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: signal_manager.cpp
 *
 * Purpose: signal distribute helper API implementation for PandaOS
 *
 * Developer:
 *   wen.gu , 2020-09-02
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "panda/sig/signal_manager.h"
#include "panda/com/client_requester.h"

namespace panda
{
namespace sig
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/

#define SIG_MANAGER_SRV_NAME "signal_manager"
#define EMIT_SIG_METHOD "emit_signal"
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

class SignalManager::impl
{
public:
    panda::com::ClientRequester* mClient = nullptr;

public:
    impl()
    {
        isInitialized();
    }
    ~impl()
    {
        if (mClient)
        {
            delete mClient;
        }
    }

    bool isInitialized()
    {
        if (!mClient)
        {
            mClient = panda::com::ClientRequester::getService(SIG_MANAGER_SRV_NAME);
            return mClient != nullptr;
        }   

        return true;
    }
};

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
SignalManager::SignalManager()
    :mImpl(new impl)
{
    /** todo something */
}

SignalManager::~SignalManager()
{
    /** todo something */
}

//static 
SignalManager& SignalManager::get()
{
    static SignalManager sm;
    return sm;
}


SignalManager::PandaErrc SignalManager::subscribeSignal(const std::string& srvName, const std::string& sigName, ClientSignalHandler sigHandler)
{
    if (mImpl->isInitialized())
    {
        std::string regSigName = srvName + "_" + sigName;
        return mImpl->mClient->subscribeSignal(regSigName, sigHandler);
    }

    return PandaErrc::NoInit;
}

void SignalManager::emitSignal(const std::string& srvName, const std::string& sigName, const Value& val)
{
    if (mImpl->isInitialized())
    {
        std::string regSigName = srvName + "_" + sigName;
        Value param;
        param["sig_name"] = srvName + "_" + sigName;
        param["data"] = val;
        mImpl->mClient->requestAsync(EMIT_SIG_METHOD, param, nullptr);
    }
}



} /** namespace sig */
} /** namespace panda */


